<template>
  <div class="swiper-container">
    <swiper class="swiper" :options="swiperOption">
      <swiper-slide v-for="item in imgList" :key="item.id">
        <img :src="item.imgUrl" alt="" @click="sendImg(item.imgUrl)" />
      </swiper-slide>
      <!-- 展示左右两边的切换箭头 -->
      <div class="swiper-button-prev" slot="button-prev"></div>
      <div class="swiper-button-next" slot="button-next"></div>
    </swiper>
  </div>
</template>

<script>
// import Swiper from 'swiper'
// 导入组件
import { Swiper, SwiperSlide } from 'vue-awesome-swiper'
// 导入swiper 相关css样式
import 'swiper/css/swiper.css'
export default {
  name: 'ImageList',
  // 将组件注册
  components: {
    Swiper,
    SwiperSlide
  },
  data() {
    return {
      // Swiper 组件的配置项，通过 props 的方式传递给 Swiper 组件
      // Swiper 组件接收到这些配置项，就能够按照配置项展示轮播图
      // 多翻阅 swiper 的官方文档
      swiperOption: {
        slidesPerView: 'auto', // 一屏展示几张 轮播图,假设是2 就是一个屏幕展示两张轮播图
        spaceBetween: 15, // 每张轮播图之间的间隙

        // 设置上一张、下一张对应的容器元素是谁
        navigation: {
          nextEl: '.swiper-button-next',
          prevEl: '.swiper-button-prev'
        }
      }
    }
  },
  props: {
    imgList: {
      type: Array
    }
  },
  methods: {
    // 通过事件总线发射一个自定义事件
    // 同时传递一个参数
    sendImg(imgUrl) {
      this.$eventBus.$emit('sendImg', imgUrl)
    }
  }
}
</script>

<style lang="less" scoped>
.swiper-container {
  height: 56px;
  width: 402px;
  box-sizing: border-box;
  // padding: 0 0px;

  .swiper-slide {
    width: 56px;
    height: 56px;

    img {
      width: 100%;
      height: 100%;
      border: 1px solid #ccc;
      padding: 2px;
      width: 50px;
      height: 50px;
      display: block;

      &.active {
        border: 2px solid #f60;
        padding: 1px;
      }

      &:hover {
        border: 2px solid #f60;
        padding: 1px;
      }
    }
  }

  .swiper-button-next {
    left: auto;
    right: 0;
  }

  .swiper-button-prev {
    left: 0;
    right: auto;
  }

  .swiper-button-next,
  .swiper-button-prev {
    box-sizing: border-box;
    width: 12px;
    height: 56px;
    background: rgb(235, 235, 235);
    border: 1px solid rgb(204, 204, 204);
    top: 0;
    margin-top: 0;
    &::after {
      font-size: 12px;
    }
  }
}
</style>
